<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Sql.class.php";
require_once dirname(__FILE__)."/Log.class.php";
require_once dirname(__FILE__)."/Inputs.class.php";
require_once dirname(__FILE__)."/Client.class.php";
require_once dirname(__FILE__)."/SessionRoster.class.php";
require_once dirname(__FILE__)."/Object_.class.php";

class ObjectContainer
{
	static private $instance;
	
	private $items = array();
	private $spells = array();
	
	private $update_max = 500;
	
	private $log;
	
	private function __construct()
	{
		$this->log = new Log('object_container');
		
		Sql::GetInstance()->Query("SET group_concat_max_len = 5000");
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new ObjectContainer();
		}
		
		return self::$instance;
	}
	
	function GetObjects($items, $spells)
	{
		Sql::GetInstance()->Query("
			SELECT
				GROUP_CONCAT(`Locale`) AS `Locales`,
				GROUP_CONCAT(`Name`) AS `Names`,
				`Color`,
				`ItemId`,
				`Texture`
			FROM
				`cp_items`
			WHERE
				`ItemId` IN ('".implode("','", $items)."')
			GROUP BY
				`ItemId`
		");
		
		while ( $item = Sql::GetInstance()->IterateResults())
		{
			$this->items[$item['ItemId']] = new Object_($item);
		}
		
		Sql::GetInstance()->Query("
			SELECT
				GROUP_CONCAT(`Locale`) AS `Locales`,
				GROUP_CONCAT(`Name`) AS `Names`,
				`Color`,
				`SpellId`,
				`Texture`
			FROM
				`cp_spells`
			WHERE
				`SpellId` IN ('".implode("','", $spells)."')
			GROUP BY
				`SpellId`
		");
		
		while ( $spell = Sql::GetInstance()->IterateResults() )
		{
			$this->spells[$spell['SpellId']] = new Object_($spell);
		}
	}
	
	function GetItem($item)
	{
		if ( array_key_exists($item, $this->items) )
		{
			return $this->items[$item];
		}
		else
		{
			return null;
		}
	}
	
	function GetSpell($spell)
	{
		if ( array_key_exists($spell, $this->spells) )
		{
			return $this->spells[$spell];
		}
		else
		{
			return null;
		}
	}
}

?>